# check for exported variable by libressl
if [[ -n "$WGET_RECAST_REQUIRED" ]] && is_depends_enabled wget $(get_spell_provider wget SSL); then
  message "${MESSAGE_COLOR}Building static wget now so that it doesn't get borked${DEFAULT_COLOR}" &&

  # make sure wget sources are in-place
  summon wget &&

  RUN_COMPILER_DIR="${RUN_COMPILER_DIR:-/var/lib/sorcery/build}" &&
  PATH="${PATH//$RUN_COMPILER_DIR:/}" &&
  (
    # this links statically against old libraries still
    export OPTS="--with-ssl=openssl --disable-pcre" && # TODO: our pcre spell doesn't install a static library yet
    export CUSTOM_CFLAGS="-static" &&
    # $4 is grimoire version, not installed version -- internal sorcery interface, too?
    local TMP_WGET_DIR="$BUILD_DIRECTORY/wget-$(gaze version wget | awk '{if (NR == 3) print $4}')" &&

    delve -c start config_loc wget &&
    # LDFLAGS+="-ldl" is required for static linking
    sedit "s:@LIBS@:@LIBS@ -ldl:" "$TMP_WGET_DIR/src/Makefile.in" &&
    # get a new wget binary built
    delve BUILD wget &&
    # and installed
    install -vm 755 "$TMP_WGET_DIR/src/wget" "$INSTALL_ROOT/usr/bin/wget"
  ) || exit 1 # for some reason 'return 1' doesn't work here
fi
